/**
 * 
 */
package game;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * Main class that handles finding out what game you want to play
 * 
 * @author Max Joyce (Jan 21,2010)
 *
 */
public class MainMenu {

	protected Game game;
	public static final Object SELECTION_LIST[] = {"Chess Game","Chess Game Vs. CPU", "Knights Tour","Eight Queens","Global Thermonuclear War"};
	public static final int GAME_SET = 1;
	public static final int GAME_NOT_SET = 0;
	/**
	 * 
	 */
	public MainMenu() { 
	}
	public void setUp(){
		setGame(displayMenu());
		game.setView(new view.ConsoleView(game.getBoard()));
	}
	/**
	 * Get the current game
	 * <pre>
	 * Max Joyce
	 * -Made the method (Jan 22,2010)
	 * </pre>
	 * @return the current game being played
	 */
	public Game getGame(){
		return this.game;
	}
	public int setGame(int gameType){
		if (gameType==1){
			//Chess game
			chess.Player p1=new chess.Player("White",game,"White");
			chess.Player p2=new chess.Player("Black",game,"Black");
			game=new ChessGame(p1,p2);
		}
		else if (gameType==2){
			//Chess game
			chess.Player p1=new chess.Player("White",game,"White");
			//chess.Player p1=new chess.BadAIChessPlayer("White",game,"White"); //This is funny
			
			chess.Player p2=new chess.BadAIChessPlayer("Black",game,"Black");
			game=new ChessGame(p1,p2);
		}
		else if (gameType==3){
			//Knight's tour game
			game = new KnightsTourGame();
		}
		else if (gameType==4){
			//8 Queens game
			game = new EightQueensGame();
		}else if (gameType == 5){
			return GAME_NOT_SET;
		}else{
			System.exit(0);
		}
		return GAME_SET;
	}
	public int displayMenu(){
		String choice="";
		BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

		
		//Pick a game
		while(!choice.equals("1") && !choice.equals("2") && !choice.equals("3") && !choice.equals("4")){
			
			System.out.println("Shall we play a game?");
			for(int i = 0; i < MainMenu.SELECTION_LIST.length;i++){
				System.out.println((i + 1) + ") " + MainMenu.SELECTION_LIST[i]);
			}
			
			try {
				choice = reader.readLine();
			} catch (IOException e) {
				e.printStackTrace();
			}
			if (choice.equals("5")){
				System.out.println("How about a nice game of chess instead?");
			}
			else if (!choice.equals("1") && !choice.equals("2") && !choice.equals("3") && !choice.equals("4")){
				System.out.println("Please enter a valid choice");
			}
		}
		try {
			int numChoice = Integer.parseInt(choice);
			return numChoice;
		} catch (NumberFormatException e) {
			System.out.println("Invalid Number");
			e.printStackTrace();
		}
		return -1;
	}
	public void playGames(){
		while(true){
			setUp();
			game.startup();
			game.play();
		}
	}
	/**
	 * Activate game now!!!!
	 * 
	 * <pre>
	 * Max Joyce 
	 * -Made the method, arbitrarily (Jan 22,2010)
	 * -Made method play a chess game, to test the game display (Jan 22,2010)
	 * </pre>
	 * 
	 */
	public static void main(String args[]){
		
		
		//Create the game
		MainMenu menu = new MainMenu();
		menu.playGames();
	}

}
